package com.explorati.LeetCode104.maxDepth;

/**
 * @ Author : Weijian_Wang
 * @ Date : Created in 22:27 2020/9/26 0026
 * @ Description ：
 */
public class Solution {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }

    /**
     * 给定一个二叉树，找出其最大深度。
     * @param root
     * @return
     */
    int depth = 0;
    public int maxDepth(TreeNode root) {
        if(root == null) {
            return depth;
        }

       maxDepth(root, 1);
        return depth;
    }

    private void maxDepth(TreeNode root, int d) {
        if(root != null) {
            if(depth < d) {
                depth = d;
            }
            if(root.left != null) {
                maxDepth(root.left, d + 1);
            }
            if(root.right != null) {
                maxDepth(root.right, d + 1);
            }
        }
    }


}
